package com.ruoyi.index.service.impl;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.ruoyi.index.domain.WeatherDto;
import com.ruoyi.index.service.IWeatherService;
import com.ruoyi.tools.HttpUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import static com.ruoyi.common.core.domain.R.ok;

@Service
public class WeatherServiceImpl implements IWeatherService {


    @Override
    public WeatherDto getWeatherData(String lat, String lon) {
        if(lat.equals("0.0")){
            WeatherDto weatherDto = new WeatherDto();
            return weatherDto;
        }
        String appcode = "e3761ef60a7e4983a1ae1c87829880d2";
        String host = "https://ali-weather.showapi.com";
        String path = "/gps-to-weather";
        String method = "GET";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("from", "5");
        querys.put("lng", lon);
        querys.put("lat", lat);
        querys.put("needMoreDay", "0");
        querys.put("needIndex", "0");
        querys.put("needHourData", "0");
        querys.put("need3HourForcast", "0");
        querys.put("needAlarm", "0");

        try {
            //发送请求
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            //获取响应实体
            HttpEntity entity = response.getEntity();
            //确保响应实体不为空
            if(entity!=null){
                //将响应实体转换为字符串
                String jsonString = EntityUtils.toString(entity);
                Gson gson = new Gson();
                //解析JSON字符串为JsonObject
                JsonObject jsonObject = JsonParser.parseString(jsonString).getAsJsonObject();
                //提取值
                JsonObject showapiResBody = jsonObject.get("showapi_res_body").getAsJsonObject();
                JsonObject now = showapiResBody.get("now").getAsJsonObject();
                //天气
                String weather = now.get("weather").getAsString();
                //温度
                String temperature = now.get("temperature").getAsString();
                //湿度
                String sd = now.get("sd").getAsString().replace("%", "");  // 去掉百分号;

                JsonObject f1 = showapiResBody.get("f1").getAsJsonObject();
                //日出时间，日落时间
                String sunBeginEnd = f1.get("sun_begin_end").getAsString();
                String weatherCode = f1.get("night_weather_code").getAsString();
                String airPress = f1.get("air_press").getAsString().replace("hPa", "").trim();

                WeatherDto weatherDto = new WeatherDto(weather, temperature, sd,sunBeginEnd,airPress,weatherCode);

//                System.out.println(EntityUtils.toString(response.getEntity()));
                return weatherDto;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
